package site.yunnong.atvris.admin.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import site.yunnong.atvris.dao.admin.form.LoginForm;
import site.yunnong.atvris.dao.admin.form.RegisterForm;
import site.yunnong.atvris.dao.admin.service.SysAdminManageService;
import site.yunnong.atvris.dao.common.api.CommonResult;
import site.yunnong.atvris.dao.common.api.YunnongHttpStatus;
import site.yunnong.atvris.dao.model.SysAdmin;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

@Api(tags = "后台用户管理")
@RestController
@RequestMapping("/admin")
@Validated
public class SysAdminController {

    @Autowired
    private SysAdminManageService sysAdminManageService;

    @Value("${jwt.tokenHead}")
    private String tokenHead;


    @ApiOperation(value = "管理员注册")
    @PostMapping("/register")
    public CommonResult<Map<String, String>> register(@Valid @RequestBody RegisterForm form) {
        String token = sysAdminManageService.register(form);
        Map<String, String> resultMap = new HashMap<>();
        resultMap.put("tokenHead", tokenHead);
        resultMap.put("token", token);
        return CommonResult.success(resultMap);
    }

    @ApiOperation(value = "管理员登录")
    @PostMapping("/login")
    public CommonResult<Map<String, String>> login(@Valid @RequestBody LoginForm form) {
        String password = form.getPassword();
        String username = form.getUsername();
        String token = sysAdminManageService.login(username, password);
        Map<String, String> resultMap = new HashMap<>();
        resultMap.put("tokenHead", tokenHead);
        resultMap.put("token", token);
        return CommonResult.result(resultMap, YunnongHttpStatus.SUCCESS);
    }

    @ApiOperation(value = "管理员信息")
    @GetMapping("/info")
    public CommonResult<SysAdmin> info(@RequestParam("adminId") Long id) {
        SysAdmin admin = sysAdminManageService.getAdminById(id);
        return CommonResult.success(admin);
    }
}
